<template>
	<view class="">
		<cu-custom :bgColor="NavBarColor" :isBack="true" backRouterName="index">
			<block slot="backText">返回</block>
			<block slot="content">选择承包商人员</block>
			<view slot="right" @tap="RouterPage()">确认</view>
		</cu-custom>
		<uni-section title="承包商人员清单">
			<uni-search-bar @confirm="search" :focus="true" v-model="searchValue" @blur="blur" @focus="focus"
				@input="input" @cancel="cancel" @clear="clear" placeholder="输入搜索的关键词">
			</uni-search-bar>
			<view class="uni-list" v-if="isShow">
				<uni-data-checkbox v-model="selectIds" :localdata="msgList" multiple></uni-data-checkbox>
				<!-- <checkbox-group @change="selectChange">
					<label class="width uni-list-cell uni-list-cell-pd" v-for="item in msgList" :key="item.value">
						<view class="change">
							{{item.checked}}
							<checkbox :value="item.value" :checked="item.checked" />
						</view>
						<view class="uesrname">{{item.name}}</view>
					</label>
				</checkbox-group> -->
			</view>
			<view v-else>暂无数据</view>
		</uni-section>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchValue: '',
				searchName: [],
				msgList: [],
				isShow: true,
				inputVlue: '',
				current: '',
				selectIds: [],

				model: {}, //对象啥都可以传
				url: {
					//list: '/TbContractor/tbContractor/list',
					list: '/TbContractorPersonnel/tbContractorPersonnel/list',
					img: "../../static/icon/user-line.png"
				}
			}
		},
		watch: {
			inputVlue(newVlue, oldVlue) {
				this.searchName = []
			}
		},
		onLoad() {
			this.initmsgList(); //初始化人员消息
		},
		methods: {
			initmsgList() {
				this.$http.get(this.url.list,{params:{pageNo: 1, pageSize:1000}}).then((res) => {
					if (res.data.success) {
						if (res.data.result.records.length > 0) {
							res.data.result.records.forEach(item =>{
								this.msgList.push({
									value: item.id,
									name: item.name,
									text: item.name + "("+item.contractorId_dictText+")"
								});
							})
						}
					}
				});
			},
			search(res) {
				this.msgList.forEach((item) => {
					if (item.name.indexOf(this.searchValue) !== -1) {
						this.searchName.push(item)
					}
				})
				if (this.searchName.length > 0) {
					this.msgList = this.searchName
					this.isShow = true
				} else {
					this.isShow = false
				}
				// uni.showToast({
				// 	title: '搜索：' + res.value,
				// 	icon: 'none'
				// })
			},
			input(res) {
				this.inputVlue = res
				if (res == '') {
					this.initmsgList();
					this.isShow = true
				}
			},
			clear(res) {
				this.initmsgList();
				this.isShow = true
				// uni.showToast({
				// 	title: 'clear事件，清除值为：' + res.value,
				// 	icon: 'none'
				// })
			},
			blur(res) {
				// uni.showToast({
				// 	title: 'blur事件，输入值为：' + res.value,
				// 	icon: 'none'
				// })
			},
			focus(e) {
				// uni.showToast({
				// 	title: 'focus事件，输出值为：' + e.value,
				// 	icon: 'none'
				// })
			},
			cancel(res) {
				// uni.showToast({
				// 	title: '点击取消，输入值为：' + res.value,
				// 	icon: 'none'
				// })
			},
			selectChange: function(evt) {
				var items = this.msgList,
					values = evt.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					console.log(values+"========-----========"+JSON.stringify(item))
					if(values.includes(item.value)){
						this.$set(this.msgList[i],'checked',true)
					}else{
						this.$set(this.msgList[i],'checked',false)
					}
				}
				
				console.log("========-----========"+JSON.stringify(this.msgList))
			},
			RouterPage() { //必须要这么写要保证上一个页面数据不会丢失
				/* 解决方案参考连接https://blog.csdn.net/Joye_7y/article/details/125648530 */
				let pages = getCurrentPages()
				// 2. 上一页面实例
				// 注意是length长度，所以要想得到上一页面的实例需要 -2
				// 若要返回上上页面的实例就 -3，以此类推
				let prevPage = pages[pages.length - 2];
				// 3. 给上一页面实例绑定getValue()方法和参数（注意是$vm）
				
				console.log("======sselectIds========"+this.selectIds)
				var selectRows = [];
				if(this.selectIds.length >0){
					this.selectIds.forEach(id =>{
						this.msgList.forEach(item =>{
							if(id == item.value){
								selectRows.push(item);
							}
						})
					})
				}
				prevPage.$vm.getUserCheckBoxValue(selectRows);

				// 4. 返回上一页面
				uni.navigateBack({
					delta: 1 // 返回的页面数
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-result {
		padding-top: 10px;
		padding-bottom: 20px;
		text-align: center;
	}

	.search-result-text {
		text-align: center;
		font-size: 14px;
		color: #666;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		padding: 0px;
	}

	.uni-mt-10 {
		margin-top: 10px;
	}

	.cu-form-group image {
		width: 42px;
		height: 42px;
	}

	/deep/.uni-section-header {
		display: none !important;
	}
	.width{
		line-height: 38px;
	}
	.viewClass{
		width: 80%;
		display: initial;
		float: left;
	}
	.viewClass span{
		float: left;
	}
	.change{
		width: 15%;
		margin-left: 5%;
		display: initial;
		float: left;
	}
	
</style>